package com.foo.web.controller.api;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import com.foo.common.enums.OperatorType;
import com.foo.system.service.ISysOperLogService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.foo.common.annotation.Log;
import com.foo.common.enums.BusinessType;
import com.foo.wechat.domain.WxConfig;
import com.foo.wechat.service.IWxConfigService;
import com.foo.common.core.controller.BaseController;
import com.foo.common.core.domain.AjaxResult;

/**
 * 项目配置Api
 * 
 * @author foo
 * @date 2020-04-30
 */
@Controller
@RequestMapping("/wechat/api/config")
public class WxConfigApi extends BaseController
{
    @Autowired
    private IWxConfigService wxConfigService;

    private String prefix = "monitor/operlog";

    @Autowired
    private ISysOperLogService operLogService;

    /**
     * 查询项目配置列表
     */
    @GetMapping("/list")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult list(WxConfig wxConfig,Integer pageNum, Integer pageSize)
    {
        if(pageNum != null){
            Integer size = 10;
            if(pageSize != null){
                size = pageSize;
            }
            PageHelper.startPage(pageNum, size);
        }
        List<WxConfig> list = wxConfigService.selectWxConfigList(wxConfig);
        return AjaxResult.success(list);
    }

    /**
     * 新增项目配置
     */
    @PostMapping("/add")
    @ResponseBody
    @Log(title = "项目配置", businessType = BusinessType.INSERT, operatorType = OperatorType.MOBILE)
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addSave(@RequestBody WxConfig wxConfig) throws Exception
    {
        if(wxConfigService.insertWxConfig(wxConfig) > 0){
            List<WxConfig> wxConfigList = wxConfigService.selectWxConfigList(wxConfig);
            if(wxConfigList.size() > 0){
                return AjaxResult.success(wxConfigList.get(0));
            }else{
                //数据添加成功之后查询不到数据 回滚
                throw new SQLException("数据为空");
            }
        }
        return toAjax(0);
    }

    /**
     * 修改项目配置
     */
    @PostMapping("/edit")
    @ResponseBody
    @Log(title = "项目配置", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE)
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult editSave(@RequestBody WxConfig wxConfig)
    {
        return toAjax(wxConfigService.updateWxConfig(wxConfig));
    }

    /**
     * 删除 项目配置
     */
    @PostMapping( "/remove")
    @ResponseBody
    @Log(title = "项目配置", businessType = BusinessType.DELETE, operatorType = OperatorType.MOBILE)
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(@RequestBody String id)
    {
        return toAjax(wxConfigService.deleteWxConfigByIds(id));
    }

    @GetMapping("/detail/{operId}")
    public String detail(@PathVariable("operId") String operId, ModelMap mmap)
    {
        mmap.put("operLog", operLogService.selectOperLogById(operId));
        return prefix + "/detail";
    }

    /**
     * 查询是否开启系统维护
     * @return
     */
    @GetMapping( "/getIsUpdate")
    @ResponseBody
    public AjaxResult getIsUpdate(){
        return AjaxResult.success(wxConfigService.selectConfigByKey("wx_app_update"));
    }
}
